博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
request和response的知识
阅读量:4323 次
发布时间:2019-06-06

本文共 6295 字,大约阅读时间需要 20 分钟。

[java] view plain copy

public class Demo1 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      doPost(request, response);  }    //在servlet中用outputstream输出中文的问题  public void doPost(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      test4(response);  }  private void test4(HttpServletResponse response)  throws IOException, UnsupportedEncodingException {      //如果servle的代码写成这样"response.getOutputStream().write(1);",因为      //浏览器默认的编码是gb2312,那么它会去寻找编号为1所对应的字符,结果是为"",      //如果写成字符串"1"的话,那么这个1字符是经过getBytes之后的,所以会直接输出1      response.getOutputStream().write(1);      response.getOutputStream().write("1".getBytes());  }  private void test3(HttpServletResponse response)  throws IOException, UnsupportedEncodingException {      String name="中国3";      //如果程序把text/html后面的;写成了,的话,那么浏览器会提示下载此servlet文件      response.setHeader("Content-type", "text/html,charset=UTF-8");      response.getOutputStream().write(name.getBytes("UTF-8"));  }  private void test2(HttpServletResponse response)      throws IOException, UnsupportedEncodingException {      String name="中国2";      //用html技术中的meta标签来模拟http的响应头,来控制浏览器的行为      response.getOutputStream().write("
".getBytes()); response.getOutputStream().write(name.getBytes("UTF-8")); } private void test1(HttpServletResponse response) throws IOException, UnsupportedEncodingException { String name="中国1"; //程序以什么码表输出了,程序就要控制浏览器以什么样的码表打开 response.setHeader("Content-type", "text/html;charset=UTF-8"); //response.getOutputStream().write(name.getBytes()); response.getOutputStream().write(name.getBytes("UTF-8")); }

}

[java] view plain copy
public class Demo2 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      doPost(request, response);  }

//通过response的wirter流输出数据

public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//因为在servlet传递给浏览器的过程中是通过response进行编码后传递的,而老外默认是
//使用iso8859-1来进行编码传递的,所以我们需要对response的编码进行设置,以控制
//response以什么码表向浏览器写出数据
//测试得知,在设置response的编码时最好写在上面

//第一种方式,控制response的编码和浏览器显示的编码,因为浏览器默认是gb2312的      //response.setCharacterEncoding("utf-8");      //下面这句通过response设置浏览器的编码,其实默认同时也把response的编码也给设置了,所以上面的那句话也可以省略掉了      //response.setContentType("text/html;charset=utf-8");            //第二种方式,控制response的编码与浏览器的一致,也就是gb2312编码      response.setCharacterEncoding("gb2312");            String name="中国";      PrintWriter out=response.getWriter();      //第三种方式,不设置response的编码,使用默认的iso8859-1,然后把string转化为8859-1后进行传递      //out.print(new String(name.getBytes(),"iso8859-1"));            out.print(name);        }

}

[java] view plain copy

//文件下载
public class Demo3 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      doPost(request, response);  }  public void doPost(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      String path=this.getServletContext().getRealPath("/download/小破孩.jpg");      String filename=path.substring(path.lastIndexOf("/")+1);      //如果下载文件是中文文件,那么文件名需要经过url编码      response.setHeader("content-disposition", "attachment;filename="+URLEncoder.encode(filename,"utf-8"));      InputStream is=new FileInputStream(path);      OutputStream os=response.getOutputStream();      int len=0;      byte[] bs=new byte[1024];      while((len=is.read(bs))>0){          os.write(bs, 0, len);      }      os.close();      is.close();  }

}

[java] view plain copy

public class Demo5 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      doPost(request, response);  }  public void doPost(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      //test1(response);      //test2(response);      test3(request,response);  }  //实用的跳转技术,最终的信息还是要在浏览器中显示比较好,这样的话容易排版,test2中是输出的是直接页面的源代码  private void test3(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {            String message="
恭喜你,登录成功了,本浏览器将在3秒后,跳到首页,如果没有跳,请点击此处"; request.setAttribute("message", message); this.getServletContext().getRequestDispatcher("/forword.jsp").forward(request, response); } private void test2(HttpServletResponse response) throws IOException { //假设这是一个用于登录的Servlet //假设程序运行到此,用户登录成功了 response.setContentType("text/html;charset=gb2312"); response.setHeader("refresh", "3;url='/testweb/index.jsp'"); response.getWriter().write("恭喜你,登录成功了,本浏览器将在3秒后,跳到首页,如果没有跳,请点击此处"); } private void test1(HttpServletResponse response) throws IOException { response.setHeader("refresh", "3");//每隔三秒刷新一次 int data=new Random().nextInt(1000); response.getWriter().println(data); }

}

[java] view plain copy

public class Demo5 extends HttpServlet {

public void doGet(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      //使用expries缓存当前内容      response.setDateHeader("expires", System.currentTimeMillis()+1000*3600);      String data="bbbbbbbbbbbbbbbbbb";      System.out.println("访问---");      response.getWriter().write(data);  }  public void doPost(HttpServletRequest request, HttpServletResponse response)          throws ServletException, IOException {      doGet(request, response);  }

}

[java] view plain copy

/**

  • 重定向的特点:
  • 1.浏览器会向服务器发送两次请求,意味着就有两个request/response
  • 2.用重定向技术,地址栏会发生变化
  • *用户登录和购物车时,通常会用到重定向技术
  • 同时调用getOutPutStream()和getWriter()会抛出异常

    /
    public class Demo6 extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {
    /sendRedirect的内部原理
    response.setStatus(302);
    response.setHeader("location", "/testweb/index.jsp");
    /
    response.sendRedirect("/testweb/index.jsp");
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)

    throws ServletException, IOException {
    doGet(request, response);
    }

}

转载于:https://www.cnblogs.com/cyy-13/p/5713881.html

你可能感兴趣的文章
阶段3 2.Spring_03.Spring的 IOC 和 DI_4 ApplicationContext的三个实现类
查看>>
阶段3 2.Spring_02.程序间耦合_8 工厂模式解耦的升级版
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_6 spring中bean的细节之三种创建Bean对象的方式
查看>>
阶段3 2.Spring_03.Spring的 IOC 和 DI_3 spring基于XML的IOC环境搭建和入门
查看>>
阶段3 2.Spring_04.Spring的常用注解_3 用于创建的Component注解
查看>>
阶段3 2.Spring_04.Spring的常用注解_2 常用IOC注解按照作用分类
查看>>
阶段3 2.Spring_04.Spring的常用注解_5 自动按照类型注入
查看>>
阶段3 2.Spring_04.Spring的常用注解_7 改变作用范围以及和生命周期相关的注解
查看>>
阶段3 2.Spring_05.基于XML的IOC的案例1_3 测试基于XML的IOC案例
查看>>
阶段3 2.Spring_04.Spring的常用注解_4 由Component衍生的注解
查看>>
阶段3 2.Spring_06.Spring的新注解_2 spring的新注解-Bean
查看>>
阶段3 2.Spring_04.Spring的常用注解_6 用于注入数据的注解
查看>>
阶段3 2.Spring_06.Spring的新注解_3 AnnotationConfigApplicationContext的使用
查看>>
阶段3 2.Spring_07.银行转账案例_2 案例中添加转账方法并演示事务问题
查看>>
阶段3 2.Spring_07.银行转账案例_6 测试转账并分析案例中的问题
查看>>
阶段3 2.Spring_07.银行转账案例_7 代理的分析
查看>>
阶段3 2.Spring_07.银行转账案例_3 分析事务的问题并编写ConnectionUtils
查看>>
阶段3 2.Spring_07.银行转账案例_9 基于子类的动态代理
查看>>
阶段3 2.Spring_08.面向切面编程 AOP_1 AOP的概念
查看>>
阶段3 2.Spring_08.面向切面编程 AOP_4 spring基于XML的AOP-配置步骤
查看>>